// src/api/chat/session.ts
import request from '@/utils/request'

export interface SysUserVo {
  id: number
  account: string
  nickname: string
  avatarUrl?: string
  status?: number
  lastSeenAt?: string
  createdAt?: string
  updatedAt?: string
  // 可按后端实际增加 token、角色等
}

export interface ChatSession {
  id: number
  sessionType: 'single' | 'group'
  name?: string
  ownerId?: number
  lastMessageId?: number
  lastMessageAt?: string
  sysUserVo?: SysUserVo
  createdAt?: string
  updatedAt?: string
}

export interface CreateSingleParams {
  userA: number
  userB: number
}

export interface CreateGroupParams {
  ownerId: number
  name: string
  members: number[] // Body(JSON)：成员ID数组
}

export interface SessionListParams {
  userId: number
}

/**
 * 创建单聊会话
 * POST /api/chat/session/create/single
 * @param {CreateSingleParams} data { userA, userB }
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":ChatSession}
 */
export function createSingleSession(data: CreateSingleParams): Promise<any> {
  return request({
    url: '/api/chat/session/create/single',
    method: 'post',
    data,
  })
}

/**
 * 创建群聊会话
 * POST /api/chat/session/create/group
 * @param {CreateGroupParams} data { ownerId, name, members([]) }
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":ChatSession}
 */
export function createGroupSession(data: CreateGroupParams): Promise<any> {
  return request({
    url: '/api/chat/session/create/group',
    method: 'post',
    data,
  })
}

/**
 * 我参与的会话列表
 * GET /api/chat/session/list
 * @param {SessionListParams} params { userId }
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":ChatSession[]}
 */
export function listMySessions(params: SessionListParams): Promise<any> {
  return request({
    url: '/api/chat/session/list',
    method: 'get',
    params,
  })
}

/**
 * 会话成员列表
 * GET /api/chat/session/{id}/members
 * @param {number|string} id 会话ID
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":Array<ChatSessionMember>}
 */
export function listSessionMembers(id: number | string): Promise<any> {
  return request({
    url: `/api/chat/session/${id}/members`,
    method: 'get',
  })
}

/**
 * 删除会话
 * GET /api/chat/session/deleteSingle
 * @param {SessionListParams} params { sessionId }
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":[]}
 */
export function deleteSingle(params: SessionListParams): Promise<any> {
  return request({
    url: '/api/chat/session/deleteSingle',
    method: 'get',
    params,
  })
}
